package com.itboye.mengwu.app.ui.mymeng;

import android.content.Intent;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import java.util.Observable;
import java.util.Observer;

import com.itboye.app.service.util.UpdateManager;
import com.itboye.app.service.util.UpdateManagerOld;
import com.itboye.core.alert.ByAlert;
import com.itboye.core.object.BaseActivity;
import com.itboye.core.object.ResultEntity;
import com.itboye.mengwu.R;
import com.itboye.mengwu.app.ui.web.WebActivity;
import com.itboye.mengwu.data.model.GetVersionBean;
import com.itboye.mengwu.data.presenter.MessgePresenter;
import com.itboye.mengwu.data.presenter.NewMaMaZengPresenter;
import com.itboye.mengwu.data.presenter.PrivatePresenter;

/**
 * @since 2016-05-10
 * @author liutiantian 关于萌屋
 * 
 */
public class AboutActivity extends BaseActivity implements OnClickListener,
		Observer {

	private TextView banben_num;
	private RelativeLayout realyoutNews;
	ImageView reback;
	TextView user_text;

	PrivatePresenter presenter;

	GetVersionBean getVersionBean = new GetVersionBean();

	String name;

	@Override
	public int initView() {
		// TODO Auto-generated method stub
		return R.layout.about;
	}

	@Override
	public void onMyCreate() {
		// TODO Auto-generated method stub
		banben_num.setText(getAppVersionName(this));
		name = getAppVersionName(this);

		presenter = new PrivatePresenter(this);

		// if (name == name) {
		// // banben_num.setText("最新版本了");
		// } else if (name != name) {
		// banben_num.setText(name);
		// }

		realyoutNews.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {

				presenter.getVersion();
				showProgressDialog("正在查看是否有新的版本", true);

			}
		});
		reback.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				finish();
			}
		});
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.user_text:
			Intent intent = new Intent(this, WebActivity.class);
			intent.putExtra("title", "用户协议");
			intent.putExtra("url",
					"http://www.moodwo.com/Public/html/copyright.html");
			startActivity(intent);
			break;

		default:
			break;
		}
	}

	@Override
	public void update(Observable observable, Object data) {
		ResultEntity entity = super.handlerError(data);
		// ByAlert.alert("><" + entity.getData());
		if (entity != null) {
			if (entity.getEventType() == PrivatePresenter.version_success) {

				getVersionBean = (GetVersionBean) entity.getData();
				Log.e("hu", getVersionBean.getVersion() + "");
				if (name.equals(getVersionBean.getVersion())) {
					ByAlert.alert("已经是最新版本~");
				} else {
					ByAlert.alert("现在不是最新版本，还需要更新~");

					// UpdateManagerOld manager = new
					// UpdateManagerOld(AboutActivity.this);
					// // 检查软件更新
					// manager.checkUpdate();
					UpdateManager updateManager = new UpdateManager(this,
							getVersionBean.getUpdateLog(), getVersionBean
									.getDownloadUrl().get(0).getValue());
					updateManager.checkUpdateInfo();
				}
			} else if (entity.getEventType() == PrivatePresenter.version_fail) {

			}
		}

		try {
			closeProgressDialog();
		} catch (Exception e) {
			// TODO: handle exception
		}
	}
}
